'''

Case Type： 数据库系统
Case Name： 数据库正常运行，修改数据库实例目录权限
Create At:  2020/07/20
Owner    :  @li-xin12345
Descption:  测试数据库正常运行，修改数据库实例目录权限是否影响数据库关闭
            1.修改数据库实例目录 ，去除其读权限
            2.关闭数据库
history：
created： 2020/07/20  created by @li-xin12345
'''

import os
import unittest
from yat.test import Node
from testcase.utils.CommonSH import *
import sys
import time
from yat.test import macro
from testcase.utils.CommonSH import *

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()
sys.path.append(sys.path[0] + "/../")


class IndexFileDamaged(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_stadb = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info('------------Opengauss_Reliability_Dbsys_Case206.py start------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.rootNode = Node('default')
        self.primaryNode = Node(node='PrimaryDbUser')
        self.standbyNode = Node(node='Standby1DbUser')
        self.Constant = Constant()

    def test_Index_file_damaged(self):
        # 更改数据库实例目录权限
        logger.info('----------------------------去除数据库实例目录读权限-----------------------------')
        excute_cmd = f'chmod -R u-r {self.DB_INSTANCE_PATH} ;'
        logger.info(excute_cmd)
        msg = self.rootNode.sh(excute_cmd).result()
        logger.info(msg)

        # 关闭数据库验证是否受数据库实例目录权限影响
        logger.info('----------------------------关闭数据库-----------------------------')
        excute_cmd = f'''
                        source {self.DB_ENV_PATH};
                        gs_om -t stop;
                        '''
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        # self.assertIn(self.Constant.STOP_FAIL_MSG, msg)

        # 恢复数据库实例目录权限
        logger.info('----------------------------增加数据库实例目录读权限-----------------------------')
        excute_cmd = f'chmod -R u+r {self.DB_INSTANCE_PATH} ;'
        logger.info(excute_cmd)
        msg = self.rootNode.sh(excute_cmd).result()
        logger.info(msg)

        # 重启数据库验证数据库正常
        logger.info('----------------------------重启数据库-----------------------------')
        excute_cmd = f'''
                    source {self.DB_ENV_PATH};
                    gs_om -t stop;
                    gs_om -t start;
                    '''
        logger.info(excute_cmd)
        msg = self.primaryNode.sh(excute_cmd).result()
        logger.info(msg)
        self.assertIn(self.Constant.START_SUCCESS_MSG, msg)

    def tearDown(self):
        logger.info('----------------------------后置处理-----------------------------')
        try:
            db_status = self.sh_primy.getDbInstanceStatus()
            logger.info(db_status)
            if db_status:
                pass
            else:
                is_started = self.sh_primy.startDbCluster()
                self.assertTrue(is_started)
                logger.info(f'{is_started}')
                logger.info(f'db_status: {is_started}')
        finally:
            excute_cmd = f'chmod -R u+r {self.DB_INSTANCE_PATH} ;'
            logger.info(excute_cmd)
            msg = self.rootNode.sh(excute_cmd).result()
            logger.info(msg)
        logger.info('-----------------------Reliability_Dbsys_Case206执行完成-----------------------------')
